package org.bot.server.flow.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.bot.server.client.entity.BotClient;
import org.bot.server.flow.entity.BotFlow;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author 沧月
 * @create 0021 2023/9/21 13:58
 * @description <description class purpose>
 */
@Mapper
@Repository
public interface BotFlowMapper extends BaseMapper<BotFlow> {

    @Select("""
                        <script>select bot_flow.cron,bot_flow.id,bot_flow.flow_name,bot_flow.creation_date,bot_flow.status,bot_flow.flow_type,
                        bot_client.name,bot_client.client_id,bot_client.id as client_id_index from bot_flow left join bot_client on bot_flow.client_id = 
                        bot_client.id 
                        <where>
                            <if test='name!=null'> flow_name like concat('%',#{name},'%') </if>
                            <if test='clientId !=null'> and bot_flow.client_id = #{clientId} </if>
                        </where> 
                        order by bot_flow.id desc limit #{page},#{size}</script>
            """)
    List<Map<String, Object>> list(@Param("name") String name, @Param("clientId") Integer clientId, @Param("page") int page, @Param("size") int size);

    @Select("<script>select count(1) as num from bot_flow <where><if test='name!=null'> flow_name like concat('%',#{name},'%') </if></where></script>")
    int listCount(@Param("name") String name);

    @Select("select * from bot_client where status <> '3'")
    List<BotClient> clientList();

    @Select("select * from bot_client where id = #{clientId} limit 1")
    BotClient getBotClientInfo(@Param("clientId") Integer clientId);


    @Select("select bf.id,bf.flow_name,bf.client_id,bf.cron,bf.status,bf.flow_type,bc.status as client_status " +
            "from bot_flow bf inner join bot_client bc on bc.id = bf.client_id " +
            "where bc.client_id = #{clientId}")
    List<Map<String, Object>> jobClientTask(@Param("clientId") String clientId);


    @Select("select bf.id,bf.flow_name,bf.client_id,bf.cron,bf.status,bf.flow_type,bc.status as client_status " +
            "from bot_flow bf inner join bot_client bc on bc.id = bf.client_id where bf.id = #{flowId} limit 1")
    Map<String, Object> jobFlowTask(@Param("flowId") String flowId);


    @Select("select id,flow_name,client_id,cron,status,flow_type from bot_flow where status = 1 and flow_type = 2 and cron is not null")
    List<Map<String, Object>> getAllAutoTask();
}
